package pers.lance.firstweb.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@WebServlet(name = "DownloadServlet", urlPatterns = "/downloadImg")
public class DownloadServlet extends UserInterceptServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Content-Disposition", "attachment; filename=foobar.pdf");
        String imagesPath = this.getServletContext().getRealPath("/");

        OutputStream out = response.getOutputStream();
        InputStream in = new FileInputStream(imagesPath + "/WEB-INF/imgs/kauai.jpg");
        byte[] buf = new byte[1024];
        int len = 0;

        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
        in.close();
        out.flush();
    }
}
